package com.l024.android_ljl.viewmodel.main;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.entity.MessageWrap;
import com.l024.android_ljl.entity.ObjectEntity;
import com.l024.android_ljl.entity.SysCommentEntity;
import com.l024.android_ljl.entity.SysSubjectEntity;
import com.l024.android_ljl.entity.SysTypeEntity;
import com.l024.android_ljl.entity.SysUserSaveEntity;
import com.l024.android_ljl.repository.AppRepository;
import com.l024.android_ljl.view.main.activity.SubjectActivity;
import com.quantstack.llcommonlibrary.base.BaseHttpResult;
import com.quantstack.llcommonlibrary.base.BaseObserver;
import com.quantstack.llcommonlibrary.base.BaseViewModel;
import com.quantstack.llcommonlibrary.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/5/7 15:08
 * @Notes
 */
public class SubjectViewModel extends BaseViewModel<SysSubjectEntity> {
    public MutableLiveData<List<SysSubjectEntity>> subject;
    public MutableLiveData<List<SysTypeEntity>> types;//所有类型
    public MutableLiveData<List<SysCommentEntity>> comments;//所有评论
    public MutableLiveData<List<SysUserSaveEntity>> userCollections;//所有收藏
    public MutableLiveData<List<SysCommentEntity>> userComments;//用户所有评论
    private AppRepository appRepository;
    private MutableLiveData<Integer> currentIndex;
    private MutableLiveData<SysSubjectEntity> currentSubject;//当前的试题
    private MutableLiveData<Boolean> show;//是否显示
    private MutableLiveData<Boolean> isEnd;//是否答题完了
    private MutableLiveData<Integer> integral;//分数
    private MutableLiveData<String> currentAnswer;//挡圈回答
    private MutableLiveData<String> currentComment;//当前评论


    public SubjectViewModel(@NonNull @NotNull Application application) {
        super(application);
        appRepository = new AppRepository();
    }

    public MutableLiveData<List<SysSubjectEntity>> getSubject() {
        if(subject==null){
            subject = new MutableLiveData<>();
            subject.setValue(new ArrayList<>());
        }
        return subject;
    }

    public void setSubject(List<SysSubjectEntity> subject) {
        getSubject().setValue(subject);
    }

    public MutableLiveData<Integer> getCurrentIndex() {
        if(currentIndex==null){
            currentIndex = new MutableLiveData<>();
            currentIndex.setValue(0);
        }
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        getCurrentIndex().setValue(currentIndex);
    }

    public MutableLiveData<SysSubjectEntity> getCurrentSubject() {
        if (currentSubject==null){
            currentSubject = new MutableLiveData<>();
            currentSubject.setValue(new SysSubjectEntity());
        }
        return currentSubject;
    }

    public void setCurrentSubject(SysSubjectEntity currentSubject) {
       getCurrentSubject().setValue(currentSubject);
    }

    public MutableLiveData<Boolean> getShow() {
        if(show==null){
            show = new MutableLiveData<>();
            show.setValue(false);
        }
        return show;
    }

    public void setShow(Boolean show) {
        getShow().setValue(show);
    }

    public MutableLiveData<Integer> getIntegral() {
        if(integral==null){
            integral = new MutableLiveData<>();
            integral.setValue(0);
        }
        return integral;
    }

    public void setIntegral(int integral) {
        getIntegral().setValue(integral);
    }

    public MutableLiveData<Boolean> getIsEnd() {
        if(isEnd==null){
            isEnd = new MutableLiveData<>();
            isEnd.setValue(false);
        }
        return isEnd;
    }

    public void setIsEnd(boolean isEnd) {
       getIsEnd().setValue(isEnd);
    }


    public MutableLiveData<String> getCurrentAnswer() {
        if(currentAnswer==null){
            currentAnswer = new MutableLiveData<>();
            currentAnswer.setValue("");
        }
        return currentAnswer;
    }

    public void setCurrentAnswer(String currentAnswer) {
       getCurrentAnswer().setValue(currentAnswer);
    }

    public MutableLiveData<List<SysTypeEntity>> getTypes() {
        if(types==null){
            types = new MutableLiveData<>();
            types.setValue(new ArrayList<>());
        }
        return types;
    }

    public void setTypes(List<SysTypeEntity> types) {
        getTypes().setValue(types);
    }

    public MutableLiveData<List<SysCommentEntity>> getComments() {
        if(comments==null){
            comments = new MutableLiveData<>();
            comments.setValue(new ArrayList<SysCommentEntity>());
        }
        return comments;
    }

    public void setComments(List<SysCommentEntity> comments) {
        getComments().setValue(comments);
    }

    public MutableLiveData<String> getCurrentComment() {
        if(currentComment==null){
            currentComment = new MutableLiveData<>();
            currentComment.setValue("");
        }
        return currentComment;
    }

    public void setCurrentComment(String currentComment) {
        getCurrentComment().setValue(currentComment);
    }

    public MutableLiveData<List<SysUserSaveEntity>> getUserCollections() {
        if(userCollections==null){
            userCollections = new MutableLiveData<>();
            userCollections.setValue(new ArrayList<>());
        }
        return userCollections;
    }

    public void setUserCollections(List<SysUserSaveEntity> userCollections) {
        getUserCollections().setValue(userCollections);
    }

    public MutableLiveData<List<SysCommentEntity>> getUserComments() {
        if(userComments==null){
            userComments = new MutableLiveData<>();
            userComments.setValue(new ArrayList<>());
        }
        return userComments;
    }

    public void setUserComments(List<SysCommentEntity> userComments) {
        getUserComments().setValue(userComments);
    }

    /**
     * 获取所有试题
     */
    public void getSubjects(){
        addDisposable(appRepository.getSubjects(),new BaseObserver<BaseHttpResult<List<SysSubjectEntity>>>(baseView){

            @Override
            public void onSuccess(BaseHttpResult<List<SysSubjectEntity>> o) {
                if(o!=null){
                    setSubject(o.getData());
                    setCurrentIndex(0);
                    setCurrentSubject(getSubject().getValue().get(getCurrentIndex().getValue()));
                    //获取所有评论
                    getSubjectComments();
                }
                if(onPageDataListener!=null){
                    onPageDataListener.onSuccess(PageConstant.SUBJECTS);
                }
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);
                if(onPageDataListener!=null){
                    onPageDataListener.onError(PageConstant.SUBJECTS,msg);
                }
            }
        });
    }

    /**
     * 根据类型获取所有试题
     */
    public void getSubjectsByTypeId(long id){
        addDisposable(appRepository.getTypeSubjects(id),new BaseObserver<BaseHttpResult<List<SysSubjectEntity>>>(baseView){

            @Override
            public void onSuccess(BaseHttpResult<List<SysSubjectEntity>> o) {
                if(o!=null){
                    setSubject(o.getData());
                    setCurrentIndex(0);
                    setCurrentSubject(getSubject().getValue().get(getCurrentIndex().getValue()));
                }
                if(onPageDataListener!=null){
                    onPageDataListener.onSuccess(PageConstant.SUBJECTS);
                }
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);
                if(onPageDataListener!=null){
                    onPageDataListener.onError(PageConstant.SUBJECTS,msg);
                }
            }
        });
    }

    /**
     * 根据类型获取所有试题类型
     */
    public void getSubjectTypes(){
        addDisposable(appRepository.getTypes(),new BaseObserver<BaseHttpResult<List<SysTypeEntity>>>(baseView){

            @Override
            public void onSuccess(BaseHttpResult<List<SysTypeEntity>> o) {
                if(o!=null){
                    setTypes(o.getData());
                }
                if(onPageDataListener!=null){
                    onPageDataListener.onSuccess(PageConstant.SUBJECTS_TYPES);
                }
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);
                if(onPageDataListener!=null){
                    onPageDataListener.onError(PageConstant.SUBJECTS_TYPES,msg);
                }
            }
        });
    }
    /**
     * 答题
     */
    public void answer(int answer){
        String an = getCurrentSubject().getValue().getAnswer();
        String str = "A";
        switch (answer){
            case 1:{
                //判断当前答案是不是对的
                str = "A";
                break;
            }
            case 2:{
                str = "B";
                break;
            }
            case 3:{
                str = "C";
                break;
            }
            case 4:{
                str = "D";
                break;
            }

        }
        setCurrentAnswer(str);
        if(!getShow().getValue()){
            if(str.contentEquals(an)){
                ToastUtils.show("答题成功,分数+10");
                setIntegral(getIntegral().getValue()+10);
            }else{
                ToastUtils.show("答题错误！");
            }
            //显示答案
            getShow().setValue(true);
        }else{
            ToastUtils.show("不要在重复答题啦！好不好！");
        }
    }

    /**
     * 换题
     */
    public void switchAnswer(int i){
        if(i>0){
            //隐藏答案
            getShow().setValue(false);
            setCurrentAnswer("");
        }
        int index = getCurrentIndex().getValue()+i;
        int maxSize = getSubject().getValue().size();
        //索引小于0或者大于了数组长度 说明是第一或者最后一题
        if (index<0){
            ToastUtils.show("当前题目为第一题");
        }else if(index>maxSize-1){
            ToastUtils.show("当前题目为最后一题");
            //跳转到结果页面
            setIsEnd(true);
        }else{
            //更新当前索引
            setCurrentIndex(index);
            //获取当前索引对应的题目
            SysSubjectEntity sysSubjectEntity = getSubject().getValue().get(getCurrentIndex().getValue());
            //更新当前题目
            setCurrentSubject(sysSubjectEntity);
            //更新选项
            EventBus.getDefault().post(new MessageWrap(SubjectActivity.UPDATE_OPTIONS));
        }

    }

    /**
     * 用户收藏
     */
    public void savaUserSubject(){
        //获取当前用户的id和试题id
        long subjectId = getCurrentSubject().getValue().getId();
        addDisposable(appRepository.addUserSave(subjectId),new BaseObserver<ObjectEntity>(baseView){
            @Override
            public void onSuccess(ObjectEntity o) {
               ToastUtils.show(o.getObject()+"");
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);

            }
        });
    }

    /**
     * 用户评论
     */
    public void addComment(){
        //获取当前用户的id和试题id
        long subjectId = getCurrentSubject().getValue().getId();
        addDisposable(appRepository.addComment(subjectId,this.getCurrentComment().getValue()),new BaseObserver<ObjectEntity>(baseView){
            @Override
            public void onSuccess(ObjectEntity o) {
                ToastUtils.show("评论成功");
                setCurrentComment("");
                //更新
                getSubjectComments();
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);

            }
        });
    }

    /**
     * 获取当前试卷的所有评论
     */
    public void getSubjectComments(){
        long id = this.getCurrentSubject().getValue().getId();
        addDisposable(appRepository.getSubjectComments(id),new BaseObserver<BaseHttpResult<List<SysCommentEntity>>>(baseView){
            @Override
            public void onSuccess(BaseHttpResult<List<SysCommentEntity>> o) {
                if(o!=null){
                    setComments(o.getData());
                }
                if(onPageDataListener!=null){
                    onPageDataListener.onSuccess(PageConstant.SUBJECTS_COMMENTS);
                }
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);
                if(onPageDataListener!=null){
                    onPageDataListener.onError(PageConstant.SUBJECTS_COMMENTS,msg);
                }
            }
        });
    }


    /**
     * 获取用户的收藏
     */
    public void getUserCollection(){
        addDisposable(appRepository.getUserSaves(),new BaseObserver<BaseHttpResult<List<SysUserSaveEntity>>>(baseView){

            @Override
            public void onSuccess(BaseHttpResult<List<SysUserSaveEntity>> o) {
                if(o!=null){
                    setUserCollections(o.getData());
                }
                if(onPageDataListener!=null){
                    onPageDataListener.onSuccess(PageConstant.USER_COLLECTIONS);
                }
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);
                if(onPageDataListener!=null){
                    onPageDataListener.onError(PageConstant.USER_COLLECTIONS,msg);
                }
            }
        });
    }

    /**
     * 获取用户评论
     */
    public void getUserComment(){
        addDisposable(appRepository.getUserComments(),new BaseObserver<BaseHttpResult<List<SysCommentEntity>>>(baseView){
            @Override
            public void onSuccess(BaseHttpResult<List<SysCommentEntity>> o) {
                if(o!=null){
                    setUserComments(o.getData());
                }
                if(onPageDataListener!=null){
                    onPageDataListener.onSuccess(PageConstant.USER_COMMENTS);
                }
            }

            @Override
            public void onError(String msg) {
                LogUtils.e("请求错误:"+msg);
                if(onPageDataListener!=null){
                    onPageDataListener.onError(PageConstant.USER_COMMENTS,msg);
                }
            }
        });
    }

}